// #ifdef APP-PLUS
var jpushModule = uni.requireNativePlugin('JG-JPush');
// #endif

// #ifdef APP-PLUS
jpushModule.setLoggerEnable(true);
jpushModule.initJPushService();

plus.screen.lockOrientation('portrait-primary');
// 设置别名
jpushModule.setAlias({
	alias: 'test_push',
	sequence: 1,
});
//监听 极光推送连接状态

jpushModule.addConnectEventListener(result => {
	let connectEnable = result.connectEnable;
	uni.$emit('connectStatusChange', connectEnable);
	console.log('addConnectEventListener');
});
jpushModule.addNotificationListener(result => {
	//极光推送的消息通知回调
	jpushModule.setBadge(0);
	plus.runtime.setBadgeNumber(0);
	let notificationEventType = result.notificationEventType;
	let woopId = result.extras && result.extras.dataType === 'woop' && result.extras.value;
	console.log('通知', result, notificationEventType);
	// 点击事件
	if (notificationEventType == 'notificationOpened') {
		uni.navigateTo({
			url: '/pages/taskDetail/taskDetail?' + 'woopId=' + woopId,
		});
	}
});
uni.$on('connectStatusChange', connectStatus => {
	var connectStr = '';
	console.log('connectStatusChange');
	if (connectStatus == true) {
		connectStr = '已连接';
		this.getRegistrationID();
	} else {
		connectStr = '未连接';
	}
	console.log('监听到了连接状态变化2 --- ', connectStr);
	this.connectStatus = connectStr;
});
jpushModule.isPushStopped(res => {
	// code   0已停止推送  1未停止推送
	const { code } = res;
	console.log(res, '安卓连接状态2');
});
//#endif
module.exports = {
	getRegistrationID() {
		//获取registerID
		jpushModule.getRegistrationID(result => {
			let registerID = result.registerID;
			console.log(registerID);
			this.registrationID = registerID;
			uni.setStorageSync('registerID', registerID);
		});
	},
	getNotificationEnabled() {
		if (uni.getSystemInfoSync().platform == 'ios') {
			jpushModule.requestNotificationAuthorization(result => {
				let status = result.status;
				if (status < 2) {
					this.noticMsgTool();
				}
			});
		} else {
			jpushModule.isNotificationEnabled(result => {
				//判断android是否打开权限
				console.log(result);
				// if (true) {
				if (result.code == 0) {
					//如果为0则表示 未打开通知权限
					this.noticMsgTool();
				}
			});
		}
	},
	noticMsgTool() {
		if (uni.getSystemInfoSync().platform == 'ios') {
			//苹果打开对应的通知栏
			uni.showModal({
				title: '通知权限开启提醒',
				content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
				showCancel: false,
				confirmText: '去设置',
				success: function (res) {
					if (res.confirm) {
						var app = plus.ios.invoke('UIApplication', 'sharedApplication');
						var setting = plus.ios.invoke('NSURL', 'URLWithString:', 'app-settings:');
						plus.ios.invoke(app, 'openURL:', setting);
						plus.ios.deleteObject(setting);
						plus.ios.deleteObject(app);
					}
				},
			});
		} else {
			//android打开对应的通知栏
			var main = plus.android.runtimeMainActivity();
			var pkName = main.getPackageName();
			var uid = main.getApplicationInfo().plusGetAttribute('uid');
			uni.showModal({
				title: '通知权限开启提醒',
				content: '您还没有开启通知权限，无法接受到消息通知，请前往设置！',
				showCancel: false,
				confirmText: '去设置',
				success: function (res) {
					if (res.confirm) {
						var Intent = plus.android.importClass('android.content.Intent');
						var Build = plus.android.importClass('android.os.Build');
						//android 8.0引导
						if (Build.VERSION.SDK_INT >= 26) {
							var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
							intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
						} else if (Build.VERSION.SDK_INT >= 21) {
							//android 5.0-7.0
							var intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
							intent.putExtra('app_package', pkName);
							intent.putExtra('app_uid', uid);
						} else {
							//(<21)其他--跳转到该应用管理的详情页
							intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
							var uri = Uri.fromParts('package', mainActivity.getPackageName(), null);
							intent.setData(uri);
						}
						// 跳转到该应用的系统通知设置页
						main.startActivity(intent);
					}
				},
			});
		}
	},
};
